<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay;
use app\common\model\Banner;
use app\common\model\coupon\Coupon;
use app\common\model\coupon\Log;
use app\common\model\goods\Goods;
use app\common\model\MoneyLog;
use app\common\model\red\Envelope;
use app\common\model\red\Log as RedLog;
use app\common\model\ScoreLog;
use app\common\model\User;
use app\common\model\user\Log as UserLog;
use app\common\model\UserGroup;
use think\cache\driver\Redis;
use think\Db;

/**
 * 分红权益接口
 */
class Dividend extends Api
{
    protected $noNeedLogin = ['lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * 会员列表
     *
     */
    public function lists()
    {
        $list = Goods::where(['category_id' =>23])->order('level asc')->select();
        $this->success('', $list);
    }
    /**
     * 会员预览
     *
     */
    public function info()
    {
        $get = request()->get();
        extract($get);
        $this->success('', Goods::get($id));
    }
    /**
     * 会员购买
     *
     */
    public function addOrder()
    {
        $param = request()->param();
        extract($param);
        if (!$id) {
            $this->error('参数错误');
        }
        Db::startTrans();
        try {
            $data = Goods::get($id);
            $userInfo = $this->auth->getUser();
            $log = new UserLog();
            $row = $log->addLog($data, $userInfo, $pay_type);
            queue('app\common\job\Dividend', $row, 900, '1214-dividend');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('', $row);
    }

    /**
     * 立即付款
     *
     */
    public function pay()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }
        $post = $this->request->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        if (!isset($pay_type)) {
            $this->error('请选择支付方式');
        }
        try {
            $order = UserLog::get($id);
            if ($order['pay_money'] <= 0) {
                $res = $order->save(['status' => 1, 'paytime' => time()]);
                if ($res)
                    $this->success('支付成功', '', 200);
                else
                    $this->error('支付失败');
            } else {
                if ($pay_type == '0') {
                    $sdk = Pay::wxpay($order, 'dividend');
                    //微信支付
                } elseif ($pay_type == '1') {
                    //支付宝支付
                    $order['title'] = '订单支付';
                    $sdk = Pay::alipay($order);
                } elseif ($pay_type == '2') {
                    //余额支付
                    $sdk =  Pay::yue($order, $payPwd);
                } elseif ($pay_type == '3') {
                    //微信小程序支付
                    $user = User::get($order->user_id);
                    $sdk = Pay::minipay($order, $openid, 'dividend');
                }
            }
        } catch (\Exception $e) {
            $this->error('提交支付失败：' . $e->getMessage());
        }
        $this->success('', $sdk);
    }
    /**
     * 红包兑换记录
     *
     */
    public function orderlist()
    {
        $param = request()->param();
        extract($param);
        $list = RedLog::with('envelope')->where('user_id', $this->auth->id)->where('log.status', $status)->paginate(10);
        $this->success('', $list);
    }
    // /**
    //  * 红包支付
    //  *
    //  */
    // public function pay()
    // {
    //     if (!$this->request->isPost()) {
    //         $this->error('提交类型错误');
    //     }
    //     $post = $this->request->post();
    //     extract($post);
    //     if (!isset($id) || empty($id)) {
    //         $this->error('参数错误');
    //     }
    //     $order = RedLog::get($id);
    //     if ($order->state == '1') {
    //         $this->error('订单已兑换');
    //     }
    //     $res = true;
    //     try {
    //         if ($order->integral > 0) {
    //             $res = ScoreLog::income($order->user_id, $order->integral, 1, 200,  $order->sn);
    //         }
    //         $result = $order->save(['state' => 1, 'paytime' => time()]);
    //         if (!$result || !$res)
    //             exception('兑换失败');
    //     } catch (\Exception $e) {
    //         $this->error($e->getMessage());
    //     }
    //     $this->success('兑换成功');
    // }
}
